# -*- coding:utf-8 -*-
import os,sys
import re
import traceback
import time
import urllib
import urllib2
import cookielib
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.alert import Alert
from selenium.webdriver import firefox
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir, os.pardir))
import supeanut_config
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)), os.pardir))
from CommonLib.mylog import *


'''
作者：supeanut
创建时间：2016-12-11 16:06
功能描述：
	提供模拟浏览器的功能
	浏览器引擎：PhantomJS
	Python接口：Selenium
历史改动：
	2016-xx-xx: xxxxxx
'''
def getMainCloseOther(browser, url):
	browser.get(url)
	

class SeleniumCrawler:
	def __init__(self, ):
		pass


	def sample(self, ):
		browser = webdriver.PhantomJS()
		#browser.get("https://trade.foundersc.com/Fzwt_login.aspx")
		browser.get("https://www.baidu.com")
		#print browser.page_source.encode('utf-8')
		#print '---------------------------------'
		# 获取页面子对象(注意单多个元素)
		#element = driver.find_element_by_id("passwd-id")
		#element = driver.find_element_by_name("passwd")
		#element = driver.find_elements_by_tag_name("input")
		#element = driver.find_elements_by_class_name("input-class")
		#element = driver.find_element_by_xpath("//input[@id='passwd-id']")
		Elem = browser.find_element_by_id("kw")
		# 与页面子对象交互 
		# --文本框累计输入
		Elem.send_keys('5656')
		# --回车
		Elem.send_keys(Keys.RETURN)
		# --清空输入
		#Elem.clear()
		''' 下拉列表
		optElems = Elem.find_elements_by_tag_name("option")
		for optElem in optElems:
			print optElem.get_attribute("value")
			optElem.click()
		'''
		# 按钮点击
		#driver.find_element_by_id("submit-btn").click()
		# 获取弹窗对象
		#alert = driver.switch_to_alert()
		# 浏览器页面切换
		#for handle in driver.window_handles:
		#    driver.switch_to_window(handle)
		# 聚焦到name 为 child 的 frame 上
		#driver.switch_to_frame("frameName.0.child")
		# 前进后退
		# driver.forward()
		# driver.back()
		# 人为添加Cookie
		#driver.add_cookie(cookie_dict)
		# 获取cookie
		#driver.get_cookies()
		'''
		# 等待Ajax等加载出某个元素，最多等10秒
		try:
			element = WebDriverWait(driver, 10).until(
				expected_conditions.presence_of_element_located((By.ID, "myDynamicElement"))
		finally:
			loadElemTag = False
		# 最多等待一个固定时间
		driver.implicitly_wait(10) # seconds
		# 附等待条件
		title_is
		title_contains
		presence_of_element_located
		visibility_of_element_located
		visibility_of
		presence_of_all_elements_located
		text_to_be_present_in_element
		text_to_be_present_in_element_value
		frame_to_be_available_and_switch_to_it
		invisibility_of_element_located
		element_to_be_clickable – it is Displayed and Enabled.
		staleness_of
		element_to_be_selected
		element_located_to_be_selected
		element_selection_state_to_be
		element_located_selection_state_to_be
		alert_is_present
		'''
		'''
		向下滑动页面
		driver.implicitly_wait(1) # 每次滑动强行等待1秒
		while True:
			# 执行js，滑动窗口
			driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
			try:
				# 底部特征
				driver.find_element_by_xpath(".//*[@id='waterfall-loading']/img[@src='/img/no-more.png']")
				break
			except:
				pass # 继续滑动
		'''
		# 打印渲染后页面源码
		print browser.page_source.encode('utf-8')
		# 关闭选项卡
		browser.close()
		# 退出浏览器
		browser.quit()
		''' 右击，双击，拖拽，悬停
		context_click() 右击
		double_click() 双击
		drag_and_drop() 拖动
		move_to_element() 鼠标悬停
		ActionChains(browser).context_click(img3).perform()
		'''
		# 元素点击
		#driver.find_element_by_id(“xxx”).click()

		


if __name__ == '__main__':
	obj = SeleniumCrawler()

